package qj.tool.http;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;

import qj.ui.Console4;

/**
 * Created by QuanLA
 * Date: Mar 13, 2006
 * Time: 3:10:32 PM
 */
public class TempHttpClient {
    
    private static void useUrlConnection() throws IOException {
//
//        Authenticator auth = new Authenticator() {
//
//            protected PasswordAuthentication getPasswordAuthentication() {
//                if ("192.168.108.8".equals(this.getRequestingHost()))
//                    return new PasswordAuthentication("tuna", "tuna789".toCharArray());
//                else
//                    return new PasswordAuthentication("tuna", "tuna789".toCharArray());
//            }
//        };
//        Authenticator.setDefault(auth);
//
//        // Configure proxy ...
//        System.setProperty("http.proxySet", "true");
//        System.setProperty("http.proxyHost", "192.168.108.8");
//        System.setProperty("http.proxyPort", "3128");
//        System.setProperty("http.proxyType", "4");
//        
    	
        //HttpURLConnection test = (HttpURLConnection) new URL("http://192.168.108.19:9080/isis2/cmindex.do").openConnection();
        HttpURLConnection test = (HttpURLConnection) new URL("http://api-test.marketworks.com/api/apiCall.asp").openConnection();
        test.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        test.setRequestMethod("GET");
//        test.setDoOutput(true);
        
        String tempStr;
        StringBuffer sendString = new StringBuffer();
        while (!(tempStr = Console4.readLine()).equals("."))
        	sendString.append(tempStr + "\n");

        test.setRequestProperty("Content-Length", "" + sendString.length());
        
        PrintWriter printWriter = new PrintWriter(test.getOutputStream());
		printWriter.print(sendString);
		printWriter.flush();
		

        BufferedReader in = new BufferedReader (
                              new InputStreamReader (
                                test.getInputStream()
                              )
                            );

        // Read it ...
        String inputLine;
        while ((inputLine = in.readLine()) != null)
          System.out.println(inputLine);
    }

	private static void usingSocket(Socket sk, String url) throws UnknownHostException, IOException {
//		Socket sk = new Socket("displaytag.sf.net", 80);
//		Socket sk = new Socket("192.168.108.8", 8080);
		System.out.println("Connection established");
        PrintStream printStream = new PrintStream(sk.getOutputStream());

        printStream.print(
        		"GET " + url + " HTTP/1.1\r\n" +
    			"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9\r\n" +
    			"Accept: */*\r\n" +
                "\r\n");

        printStream.flush();
		System.out.println("Data sent");

        InputStream is = sk.getInputStream();
        is.read();
		System.out.println("Data Received");
		is.close();
        sk.close();
	}


}
